package com.zkingsoft.actions.admin.distribution;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DisRebateFlow;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisDesignerService;
import com.zkingsoft.services.distribution.DisMembersService;
import com.zkingsoft.services.distribution.DisRebateFlowService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Controller
@RequestMapping(value = "admin/disRebateFlow")
public class DisRebateFlowController extends BaseController {

	@Resource
	private DisRebateFlowService disRebateFlowService;

	@Resource
	private DisMembersService disMembersService;

	@Resource
	private DisDesignerService disDesignerService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "DisRebateFlow_BEV";

	public static final String fnCode = "DisRebateFlow";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DisRebateFlow disRebateFlow, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(disRebateFlowService, disRebateFlow, pageVo);
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DisRebateFlow disRebateFlow) {
		if (disRebateFlow.getFlowId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			disRebateFlow.setFlowId(((DisRebateFlow) WebUtil.getSessionAttribute(BEV)).getFlowId());
			AjaxResult result = modify(disRebateFlowService, WebUtil.getSessionAttribute(BEV), disRebateFlow, "返点记录");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			return add(disRebateFlowService, disRebateFlow, "返点记录");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DisRebateFlow disRebateFlow;
		if (id != null) {
			disRebateFlow = disRebateFlowService.findById(id);
			WebUtil.getRequest().setAttribute("obj", disRebateFlow);
			WebUtil.setSessionAttribute(BEV, disRebateFlow);
		}
		return "admin/distribution/DisRebateFlow-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(disRebateFlowService, keys);
	}

	@RequestMapping(value = "/addOrder")
	public @ResponseBody AjaxResult addOrder(DisRebateFlow disRebateFlow) {
		
		int i = disRebateFlowService.addOrder(disRebateFlow);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "订单添加成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "订单添加失败");
		}
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/findAllMonth")
	@ResponseBody
	public AjaxResult findAllMonth() {
		
		return new AjaxResult(AjaxResult.STATUS_OK, disRebateFlowService.findAllMonth());
	}
	
}